package org.homly.sql;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;

/**
 * 取数据库的连接
 * @author 吴浩
 * @version 1.0
 * 2011-6-29
 */
@SuppressWarnings("serial")
public class DbConnection {

	/**
	 * 取数据库的连接
	 * @return Connection
	 * @throws Exception
	 */
	public static Connection getConnection(String DbDriver,String DbUrl,String userName,String passWord) throws Exception {
		Connection conn = null;
		try {
              Class.forName(DbDriver).newInstance();
              conn=DriverManager.getConnection(DbUrl,userName,passWord);
		} catch (Exception e) {
            throw new Exception("链接数据库失败,请检查输入是否有误!");
		}
		return conn;
	}
	
	/**
	 * 关闭链接
	 * @throws Exception
	 */
	public static void closeAll(Connection conn, Statement stmt, ResultSet rs) throws Exception{
		try {
			if(conn!=null){
				conn.close();
			}
			if(stmt!=null){
				stmt.close();
			}
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}
}
